package se.sll.service.catalogue.rivta.service;

import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceContext;
import javax.xml.xpath.XPathExpressionException;
import lombok.extern.slf4j.Slf4j;
import se.riv.service.catalogue.findoffers.v1.rivtabp21.FindOffersResponderInterface;
import se.riv.service.catalogue.findoffers.v1.rivtabp21.FindOffersResponderService;
import se.riv.service.catalogue.findoffersresponder.v1.FindOffersResponseType;
import se.riv.service.catalogue.findoffersresponder.v1.FindOffersType;
import se.riv.service.catalogue.getcapacities.v1.rivtabp21.GetCapacitiesResponderInterface;
import se.riv.service.catalogue.getcapacities.v1.rivtabp21.GetCapacitiesResponderService;
import se.riv.service.catalogue.getcapacitiesresponder.v1.GetCapacitiesType;
import se.riv.service.catalogue.getproductcatalogue.v1.rivtabp21.GetProductCatalogueResponderInterface;
import se.riv.service.catalogue.getproductcatalogue.v1.rivtabp21.GetProductCatalogueResponderService;
import se.riv.service.catalogue.getproductcatalogueresponder.v1.GetProductCatalogueType;
import se.riv.service.catalogue.pulloffers.v1.rivtabp21.PullOffersResponderInterface;
import se.riv.service.catalogue.pulloffers.v1.rivtabp21.PullOffersResponderService;
import se.riv.service.catalogue.pulloffersresponder.v1.PullOffersType;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author khaleddaham
 */
@Path("/test")
@Slf4j
@Stateless
public class RIVTAService {

    private static final String T_MEDIA_TYPE = "application/json; charset=UTF-8";
    private static final String T_NAMESPACE = "http://ws.aggregated.catalogue.service.sll.se/";

    private FindOffersResponderService findOffersService;
    private PullOffersResponderService pullOffersService;
    private GetCapacitiesResponderService getCapacitiesService;
    private GetProductCatalogueResponderService getProductCatalogueService;
    
    @Resource WebServiceContext jaxwsContext;

    private String convertToXml(Object source, Class... type) {
        String result;
        StringWriter sw = new StringWriter();
        try {
            JAXBContext carContext = JAXBContext.newInstance(type);
            Marshaller carMarshaller = carContext.createMarshaller();
            carMarshaller.marshal(source, sw);
            result = sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    /**
     *
     * @param request
     * @param version
     * @return
     * @throws XPathExpressionException
     * @throws java.net.MalformedURLException
     */
    @POST
    @Path("/FindOffers/{version}")
    @Produces(T_MEDIA_TYPE)
    public RIVTARestResponse callFindOffers(FindOffersRequest request, @PathParam("version") String version) throws XPathExpressionException, MalformedURLException {
        FindOffersType wsRequest = new FindOffersType();
        this.findOffersService = new FindOffersResponderService(
                new URL("http://localhost:8080/aggregated/FindOffers?wsdl"),
                new QName("http://ws.aggregated.catalogue.service.sll.se/", "FindOffers"));
        FindOffersResponseType response = findOffersService.getPort(
                new QName("http://ws.aggregated.catalogue.service.sll.se/", "FindOffersPort"),
                FindOffersResponderInterface.class).findOffers("", wsRequest);
        log.info(jaxwsContext.getMessageContext().toString());
        return buildResponse(request);
    }

    /**
     *
     * @param request
     * @param version
     * @return
     * @throws XPathExpressionException
     * @throws java.net.MalformedURLException
     */
    @POST
    @Path("/GetCapacities/{version}")
    @Produces(T_MEDIA_TYPE)
    public RIVTARestResponse callGetCapacities(GetCapacitiesRequest request, @PathParam("version") String version) throws XPathExpressionException, MalformedURLException {
        GetCapacitiesType wsRequest = new GetCapacitiesType();
        this.getCapacitiesService = new GetCapacitiesResponderService(
                new URL("http://localhost:8080/aggregated/GetCapacities?wsdl"),
                new QName("http://ws.aggregated.catalogue.service.sll.se/", "GetCapacities"));
        getCapacitiesService.getPort(
                new QName(T_NAMESPACE, "GetCapacitiesPort"),
                GetCapacitiesResponderInterface.class).getCapacities("", wsRequest);
        return buildResponse(request);
    }

    /**
     *
     * @param request
     * @param version
     * @return
     * @throws XPathExpressionException
     */
    @POST
    @Path("/GetProductCatalogue/{version}")
    @Produces(T_MEDIA_TYPE)
    public RIVTARestResponse callGetProductCatalogue(GetProductCatalogueRequest request, @PathParam("version") String version) throws XPathExpressionException, MalformedURLException {
        GetProductCatalogueType wsRequest = new GetProductCatalogueType();
        this.getProductCatalogueService = new GetProductCatalogueResponderService(
                new URL("http://localhost:8080/aggregated/GetProductCatalogue?wsdl"),
                new QName("http://ws.aggregated.catalogue.service.sll.se/", "GetProductCatalogue"));
        getCapacitiesService.getPort(
                new QName(T_NAMESPACE, "GetCapacitiesPort"),
                GetProductCatalogueResponderInterface.class).getProductCatalogue("", wsRequest);
        return buildResponse(request);
    }

    /**
     *
     * @param version
     * @param start
     * @param end
     * @return
     * @throws XPathExpressionException
     */
    @POST
    @Path("/PullOffers/{version}/{start}/{end}")
    @Produces(T_MEDIA_TYPE)
    public RIVTARestResponse callPullOffers(@PathParam("version") String version, @PathParam("start") String start, @PathParam("end") String end) throws XPathExpressionException, MalformedURLException {
        PullOffersType wsRequest = new PullOffersType();
        this.pullOffersService = new PullOffersResponderService(
                new URL("http://localhost:8080/aggregated/PullOffers?wsdl"),
                new QName("http://ws.aggregated.catalogue.service.sll.se/", "PullOffers"));
        getCapacitiesService.getPort(
                new QName(T_NAMESPACE, "GetCapacitiesPort"),
                PullOffersResponderInterface.class).pullOffers("", wsRequest);
        return buildResponse(null);
    }

    /**
     *
     * @param clazz
     * @return
     */
    private RIVTARestResponse buildResponse(Object clazz) {
        RIVTARestResponse response = new RIVTARestResponse();
        response.setRequest("<xml>");
        response.setResponse("<xml>");
        return response;
    }
}
